using System;
using System.Text;
using System.Web.Security;
using System.Web.UI;

using Impact.Infrastructure.Exceptions;
using Impact.Model.Objects;
using Impact.Views;
using Impact.Web.Foundation;
using Impact.Controllers;

namespace Impact.Web.Controls
{
    public partial class DisplayArticle : Impact.Web.Foundation.DynamicPageControlBase, Impact.Views.IContentControl
    {

        /// <summary>
        /// loads the article that is to be displayed into the controls that need it
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {

            Check.Require(article, "Article is required");

            articleId.Value = article.ArticleId.ToString();
            title.Text = article.Title;
            body.Text = article.Body;
            tags.Text = GenerateTags(article);
            footer.Text = GenerateFooter(article);
            comments.Article = article;

            base.OnLoad(e);

        }

        /// <summary>
        /// Retrieves the title for the page this control is building
        /// </summary>
        protected override string PageTitle
        {
            get { return Article.Title; }
        }

        private Article article;

        /// <summary>
        /// Gets or Sets the article that will be displayed
        /// </summary>
        public Article Article
        {
            get { return article; }
            set { article = value; }
        }

        #region IContentControl Members

        /// <summary>
        /// Sets the article as the datasource for this object
        /// </summary>
        public object DataSource
        {
            set { article = (Article)value; }
        }

        /// <summary>
        /// called to perform initialization before the control loads
        /// </summary>
        public void PreInit()
        {
        }

        #endregion

        private string GenerateTags(Article article)
        {
            ArticleTagCollection tagCollection = article.Tags;
            StringBuilder tagString = new StringBuilder();
            if (tagCollection.Count > 0)
            {
                tagString.Append("Tags applied: ");
                foreach (ArticleTag tag in tagCollection)
                {
                    tagString.AppendFormat("<a href=\"{0}\">[{1}]</a>, ", this.Page.ResolveClientUrl(ContentPresenter.FormatUrl(tag)), tag.Text);
                }
            }
            return tagString.ToString();
        }


        private string GenerateFooter(Article article)
        {
            // post footer
            MembershipUser user = Membership.GetUser(new Guid(article.CreatedBy), false);
            string footer = string.Empty;
            if (user != null)
            {
                footer = string.Format("Posted by {0} on {1}.", user.UserName, article.CreatedOn.ToString("MMM d, yyyy"));
            }
            else
            {
                footer = string.Format("Posted on {0}.", article.CreatedOn.ToString("MMM d, yyyy"));
            }

            if (!string.IsNullOrEmpty(article.ModifiedBy))
            {
                MembershipUser modifyingUser = Membership.GetUser(new Guid(article.ModifiedBy), false);

                if (article.ModifiedBy.Equals(article.CreatedBy, StringComparison.CurrentCultureIgnoreCase) || modifyingUser == null)
                {
                    footer += string.Format("  Updated on {0}.", article.ModifiedOn.Value.ToString("MMM d, yyyy"));
                }
                else
                {
                    footer += string.Format("  Updated by {0} on {1}.", modifyingUser.UserName, article.ModifiedOn.Value.ToString("MMM d, yyyy"));
                }
            }

            return footer;
        }

    }
}